﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace ChouteZemUp.GameEntities
{
	public class Player : Engine.ControllerGameObject
	{
		private int mScore;
		private bool mInvicible;
		private float mBlinkTime;

        private float mLastShoot;

        protected List<Bullet> mFiredBullet;

        public int Score { get { return mScore; } }

        public Player()
            :base(Vector2.Zero, 5.0f, "Player", new Vector2(400, 400), sContent, sGame)
        {
            mInput = new Engine.PlayerController();
            mFiredBullet = new List<Bullet>();

            mRenderedRect = new Rectangle(0, 0, 128, 128);
            AddAnimation("idle", "Player", 1.0f, 3);
            Play("idle");

            mLastShoot = 0.0f;
        }

		public void Shoot()
		{

            if (mLastShoot == 0)
            {
                Bullet bullet = new Bullet(1.0f, new Vector2(0, -1), 10.0f, Position + new Vector2(base.RenderRectangle.Width / 4, 16));
                ChouteZemUp.Engine.GameObject.sGame.Components.Add(bullet);
                mFiredBullet.Add(bullet);


                bullet = new Bullet(1.0f, new Vector2(0, -1), 10.0f, Position + new Vector2(base.RenderRectangle.Width / 4 + 30, 16));
                ChouteZemUp.Engine.GameObject.sGame.Components.Add(bullet);
                mFiredBullet.Add(bullet);

                mLastShoot = 0.3f;
            }
		}

        //---------------------------

        public override void Update(GameTime pTime)
        {
            mLastShoot -= (float)pTime.ElapsedGameTime.TotalSeconds;

            if (mLastShoot < 0)
                mLastShoot = 0;

            List<Bullet> mToDelete = new List<Bullet>();

            foreach (Bullet lBullet in mFiredBullet)
            {
                Target lTarg = null;
                foreach (Target lTarget in Target.sTargetList)
                {
                    Engine.IntersectionInfo lInfo = lBullet.Intersect(lTarget);
                    if (lInfo.mCollideInfo.mCollide)
                    {
                        lTarg = lTarget;
                        lTarget.Destroy();
                        mToDelete.Add(lBullet);
                        break;
                    }
                }

                if (lTarg != null)
                {
                    mScore += lTarg.Points;
                    Target.sTargetList.Remove(lTarg);
                    break;
                }

                if (lBullet.Position.Y < -30)
                {
                    mToDelete.Add(lBullet);
                }
            }

            foreach (Bullet lBullet in mToDelete)
            {
                mFiredBullet.Remove(lBullet);
                Game.Components.Remove(lBullet);
            }

			foreach ( Target target in Target.sTargetList )
			{
				// si l'on touche un vaisseau ennemi
				if ( this.Intersect( target ).mCollideInfo.mCollide )
				{
					//spriteBatch.DrawString( mFont, mPlayer.Score.ToString(), Vector2.Zero, Color.White );
					Game1.sGameOver = true;
					Game.Components.Remove( this );
				}
			}

            base.Update(pTime);
        }
	}
}
